from flask import Flask, render_template,request,escape

import api
from api import geocode

app = Flask(__name__)

key = "2c5e812a00f3d177e589bd4162b15249"


@app.route('/')
def main():
    return render_template('./MainPage/index.html')


@app.route('/login')
def login():
    return render_template('./MainPage/login.html')


@app.route('/weather')
def weather():
    return render_template('./WetherPage/index.html')


@app.route('/register')
def register():
    return render_template('./MainPage/register.html')


@app.route('/walking')
def walking() -> 'html':
    return render_template('./MainPage/walking.html', the_title='智能步行导航')


@app.route('/ob', methods=['POST'])
def gongneng():
    address_origin = request.form['address_one']
    origin = geocode(key, address_origin)
    address_destination = request.form['address_two']
    destination = geocode(key, address_destination)
    results = api.walking(key, origin, destination)['route']['paths'][0]['steps']
    daohang = []
    for i in results:
        daohang.append(i['instruction'])
    return render_template('./MainPage/gongnengye.html', res=daohang, the_title='您的步行导航：', )


def log_request(req: 'flask_request', res: str) -> None:
    with open('./static/log/vsearch.log', 'a') as log:
        print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|')


@app.route('/viewlog')
def view_the_log() -> 'html':
    contents = []
    with open('./static/log/vsearch.log') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    titles = ('Form Data', 'Remote_addr', 'User_agent', 'Results')
    return render_template('./MainPage/viewlog.html',
                           the_title='View Log',
                           the_row_titles=titles,
                           the_data=contents, )


if __name__ == '__main__':
    app.run(debug=True)
